<template>
	<view class="yuyueinfo">
		<headertop title="预约信息" str1="oridei" :back1="back1"></headertop>
		<view class="topimage">

		</view>
		<view class="" style="width: 100%;margin-top: 186rpx;">
			<view class="infobox" v-if="typestr=='duoxuan'" v-for="(item,index) in yingweiarr" :key="index"
				style="margin-bottom:28rpx ;">
				<image :src="item.image" class="leftimg" mode=""></image>
				<view class="texts">
					<view class="t1">
						<view class="leftpobox" v-if="item.type==2">
							固
						</view>
						<view class="title">
							{{item.title}}
						</view>
					</view>
					<view class="t2">
						{{item.detail}}
					</view>
					<view class="addressbox">
						<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Frame@2x(40).png" mode="">
						</image>
						<span>{{item.address}}</span>
					</view>
				</view>
			</view>
			<view class="infobox" v-if="typestr!='duoxuan'">
				<image :src="contentobj.image" class="leftimg" mode=""></image>
				<view class="texts">
					<view class="t1">
						<view class="leftpobox" v-if="contentobj.type==2">
							固
						</view>
						<view class="title">
							{{contentobj.title}}
						</view>
					</view>
					<view class="t2">
						{{contentobj.detail}}
					</view>
					<view class="addressbox">
						<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Frame@2x(40).png" mode="">
						</image>
						<span>{{contentobj.address}}</span>
					</view>
				</view>
			</view>
		</view>


		<view class="yuyuebox">
			<view class="center">
				<view class="title">
					<view class="leftbox">

					</view>
					<view class="tt">
						预约信息
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						预约营位:
					</view>
					<view class="righttitle" v-if="typestr!='duoxuan'">
						{{contentobj.title}}
					</view>
					<view class="righttitle" v-if="typestr=='duoxuan'">
						{{yingweiarrtext()}}
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						预约时间:
					</view>
					<view class="righttitle">
						{{yuyueobj.datetime}}（{{time}}）
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						<span style=" color: #ff0000">*</span>预约人:
					</view>
					<view class="righttitle">
						<input placeholder="请输入预约人姓名(必填)" type="text" :focus="isFocused" @blur="isFocused = false"
							v-model="yuyueobj.username" />
					</view>
					<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Frame@2x(41).png" class="bianji"
						@click="isFocused=true" mode=""></image>
				</view>
				<view class="linebox">
					<view class="labletitle">
						<span style=" color: #ff0000">*</span>联系方式:
					</view>
					<view class="righttitle">
						<input placeholder="请输入手机号码(必填)" type="number" v-model="yuyueobj.telphone" />
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						<span style=" color: #ff0000">*</span>成人人数:
					</view>
					<view class="righttitle">
						<input placeholder="请输入成人人数(必填)" type="number" v-model="yuyueobj.usernum" />
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						<span style=" color: #ff0000">*</span>儿童人数:
					</view>
					<view class="righttitle">
						<input placeholder="请输入儿童人数(必填)" type="number" v-model="yuyueobj.children" />
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						备注:
					</view>
					<view class="righttitle">
						<input placeholder="请输入备注" v-model="yuyueobj.memo" />
					</view>
				</view>
			</view>
		</view>
		<view class="yuyuebox">
			<view class="center">
				<view class="title">
					<view class="leftbox">

					</view>
					<view class="tt">
						费用信息
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						场地费:
					</view>
					<view class="righttitle" v-if="typestr!='duoxuan'">
						￥{{!userobj.level?contentobj.price:contentobj.userprice}}
					</view>
					<view class="righttitle" v-else>
						￥{{!userobj.level?cdf2:cdf1}}
					</view>
				</view>
				<view class="linebox">
					<view class="labletitle">
						押金:
					</view>
					<view class="righttitle" v-if="typestr!='duoxuan'">
						￥{{contentobj.deposit}}
					</view>
					<view class="righttitle" v-else>
						￥{{prices}}
					</view>
				</view>
			</view>
		</view>
		<view class="yuyuebox">
			<view class="center">
				<view class="title">
					<view class="leftbox">

					</view>
					<view class="tt">
						支付方式
					</view>
				</view>
				<view class="linebox1" @click="active=0">
					<view class="lefttext">
						<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11200@2x.png"
							class="iconsmall" mode="">
						</image>
						<span>微信支付</span>
					</view>
					<image
						:src="active==0?'https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11457@2x.png':'https://39gongli.oss-cn-beijing.aliyuncs.com/static/Ellipse 2638@2x.png'"
						class="xz" mode=""></image>
				</view>
				<view class="linebox1" @click="active=1">
					<view class="lefttext">
						<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11428@2x.png"
							class="iconsmall" mode="">
						</image>
						<span>余额支付（可用￥{{yueobj.money||0}}）</span>
					</view>
					<view class="right">
						<span style="margin-right:18rpx ;"
							v-if="typestr!='duoxuan'&&!userobj.level">{{(contentobj.deposit*1+contentobj.price*1)>yueobj.money?'余额不足':''}}</span>
						<span style="margin-right:18rpx ;"
							v-if="typestr!='duoxuan'&&userobj.level">{{(contentobj.deposit*1+contentobj.userprice*1)>yueobj.money?'余额不足':''}}</span>
						<span style="margin-right:18rpx ;"
							v-if="typestr=='duoxuan'&&!userobj.level">{{(prices*1+cdf*1)>yueobj.money?'余额不足':''}}</span>
						<span style="margin-right:18rpx ;"
							v-if="typestr=='duoxuan'&&userobj.level">{{(prices*1+cdf1*1)>yueobj.money?'余额不足':''}}</span>

						<image
							:src="active==1?'https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11457@2x.png':'https://39gongli.oss-cn-beijing.aliyuncs.com/static/Ellipse 2638@2x.png'"
							class="xz" mode=""></image>
					</view>
				</view>
			</view>
		</view>
		<view class="" style="height: 190rpx;">

		</view>
		<up-popup :show="show" mode="center">
			<view class="popucenter">
				<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Group 11201@2x.png"
					class="popucenterimg" mode=""></image>
				<view class="title">
					预约成功！
				</view>
				<view class="text">
					请注意查看您的订单详情，
				</view>
				<view class="text">
					提前做好出行准备，期待您的光临！
				</view>
				<view class="btn" @click="gopage()">
					返回首页
				</view>
				<view class="lookorder" @click="lookorder">
					查看订单
				</view>
			</view>
		</up-popup>
		<view class="fixedbox">
			<view class="btn" @click="ljyy">
				立即预约
			</view>
		</view>
		<userlogin ref="denglu"></userlogin>
		<sanshui-payment-password :jiage="feiyong" :val="payFlag" :digits="6" @submit="onSubmit" @cancel="onCancel" />
	</view>
</template>

<script setup>
	import SanshuiPaymentPassword from '@/components/sanshui-payment-password/sanshui-payment-password.vue'
	import userlogin from '@/components/getuser/getuser.vue'
	import {
		onPageScroll,
		onLoad
	} from "@dcloudio/uni-app";
	import {
		campfile,
		timesj,
		collect,
		addorder,
		payorder
	} from '@/api/Campposition.js'
	import {
		userinfo,
		lastfile
	} from '@/api/my.js'
	import {
		reactive,
		ref,
		computed
	} from 'vue';
	import headertop from '@/components/header.vue'
	import {
		walletlog
	} from '@/api/my.js'
	const back1 = ref('')
	const payoid = ref('')
	const show = ref(false)
	const payFlag = ref(false)
	const contentobj = ref({})
	const flag = ref(true)
	const userobj = ref({})
	const denglu = ref(null)
	const isFocused = ref(false)
	const time = ref('')
	const yuyueobj = ref({})
	const active = ref(-1)
	const yueobj = ref({})
	const typestr = ref('')
	const yingweiarr = ref([])
	const myInput = ref(null)
	const paypwd = ref('')
	onLoad((opt) => {
		typestr.value = opt.typestr
		console.log(opt, yuyueobj.value, 'opt------------')
		if (opt.typestr == 'duoxuan') {
			yingweiarr.value = uni.getStorageSync('yingweiarr')
			console.log(yingweiarr.value, 'yingweiarr.valueyingweiarr.valueyingweiarr.value');
			yuyueobj.value = uni.getStorageSync('yuyuesoninfo')
			uni.removeStorageSync('yingweiarr')
			uni.removeStorageSync('yuyuesoninfo')
			console.log(yuyueobj.value, 'yuyueobj.valueyuyueobj.valueyuyueobj.value');
			timeinit(yuyueobj.value.pid)
		} else {
			yuyueobj.value = JSON.parse(uni.getStorageSync('yuyuesoninfo'))
			init(opt.id)
			timeinit(opt.id)
		}
		myyue()
		getuser()
		inityuyueinfo()
	})
	async function inityuyueinfo() {
		const res = await lastfile()
		if (res.data.code == 1) {
			if (!res.data.data) return
		}
	}

	function onSubmit(e) {
		console.log(e, '输入密码到指定位数的时候自动触发的函数');
		payFlag.value = false
		paypwd.value = e.value
		ljyy()
	}

	function onCancel() {
		payFlag.value = false
		console.log('关闭');
	}
	async function getuser() {
		const res = await userinfo()
		if (res.data.code == 1) {
			userobj.value = res.data.data
		}
	}

	const feiyong = computed(() => {
		if (typestr.value == 'duoxuan') {
			var jiage = ''
			console.log(cdf2.value, cdf1.value, 'cdf1cdf1cdf1cdf1cdf2', contentobj.value, );
			if (!userobj.value.level) {
				jiage = prices.value * 1 + cdf2.value * 1
			} else {
				jiage = prices.value * 1 + cdf1.value * 1
			}
			return jiage
		} else {
			var jiage = ''
			if (!userobj.value.level) {
				jiage = contentobj.value.deposit * 1 + contentobj.value.price * 1
			} else {
				jiage = contentobj.value.deposit * 1 + contentobj.value.userprice * 1
			}
			return jiage
		}

	})

	const cdf = computed(() => {
		var jine = yingweiarr.value.reduce((e, item) => {
			return e + item.deposit * 1
		}, 0)
		return jine
	})
	const cdf1 = computed(() => {
		var jine = yingweiarr.value.reduce((e, item) => {
			return e + item.userprice * 1
		}, 0)
		return jine
	})
	const cdf2 = computed(() => {
		var jine = yingweiarr.value.reduce((e, item) => {
			return e + item.price * 1
		}, 0)
		return jine
	})
	const prices = computed(() => {
		var jine = yingweiarr.value.reduce((e, item) => {
			return e + item.deposit * 1
		}, 0)
		return jine
	})

	function yingweiarrtext() {
		var arrname = yingweiarr.value.map(item => item.title)
		return arrname.join()
	}
	async function ljyy() {
		console.log(feiyong.value, 'feiyongfeiyongfeiyongfeiyongfeiyong----');
		if (!yuyueobj.value.username || !yuyueobj.value.telphone || !yuyueobj.value.usernum || !yuyueobj.value
			.children) {
			uni.showToast({
				title: '必须补全信息提交',
				icon: 'none'
			})
			return
		}
		if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(yuyueobj.value.telphone))) { //校验手机号码是否有误
			uni.showToast({
				title: '请填写正确手机号码',
				icon: "none"
			});
			return;
		}
		if (active.value == -1) {
			uni.showToast({
				title: '请选择支付方式',
				icon: 'none'
			})
			return
		}
		if (!uni.getStorageSync('token')) {
			denglu.value.DengLu()
			return
		}
		var cid = contentobj.value.id
		if (typestr.value == 'duoxuan') {
			var arrid = yingweiarr.value.map(item => item.id)
			cid = arrid.join()
		}
		if (active.value != 0) {
			if (!paypwd.value) {
				payFlag.value = true
				return
			}
		}
		const res = await addorder({
			cid,
			username: yuyueobj.value.username,
			telphone: yuyueobj.value.telphone,
			memo: yuyueobj.value.memo,
			pid: yuyueobj.value.pid,
			paytype: active.value + 1,
			datetime: yuyueobj.value.datetime,
			usernum: yuyueobj.value.usernum,
			children: yuyueobj.value.children,
			paypwd: paypwd.value,
		})
		if (res.code == 0) {
			paypwd.value = ''
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
			return
		}
		if (res.data.code == 1) {
			payoid.value = res.data.data.oid
			if (active.value == 0) {
				var payobj = res.data.data.pay
				uni.requestPayment({
					timeStamp: String(payobj.timestamp),
					nonceStr: payobj.noncestr,
					package: payobj.package,
					signType: 'MD5',
					paySign: payobj.sign,
					success(res) {
						show.value = true
					},
					fail(e) {
						setTimeout(() => {
							uni.showToast({
								title: '支付失败',
								icon: 'none'
							})
						}, 500)
						uni.navigateTo({
							url: '/sub_my/my/yingweiorderdetail?id=' + payoid.value
						})
					}
				})
			} else {
				paypwd.value = ''
				show.value = true
			}
		} else {
			uni.showToast({
				title: res.data.msg,
				icon: 'none'
			})
		}
	}

	function lookorder() {
		uni.navigateTo({
			url: '/sub_my/my/yingweiorderdetail?id=' + payoid.value
		})
	}
	async function myyue() {
		const res = await walletlog()
		if (res.data.code == 1) {
			yueobj.value = res.data.data
		}
	}
	async function init(id) {
		const res = await campfile({
			id
		})
		if (res.data.code == 1) {
			contentobj.value = res.data.data
		}
	}

	function focusInput() {
		console.log('focusInputfocusInputfocusInputfocusInputfocusInputfocusInput');
		// 调用input的focus方法
		setTimeout(() => {
			if (myInput.value) {
				myInput.value.focus();
			}
		}, 100) // 适当延迟确保组件已渲染
	}
	async function timeinit(id) {
		const res = await timesj({
			cid: id,
			date: yuyueobj.value.datetime
		})
		if (res.data.code == 1) {
			var obj = res.data.data.timelist.filter(item => item.id == yuyueobj.value['pid'])[0]
			time.value = `${obj['timestart']}~${obj['timeend']}`
		}
	}
	onPageScroll((e) => {
		if (e.scrollTop > 50) {
			back1.value = '#9DCA95'
		} else {
			back1.value = ''
		}
		console.log(e.scrollTop, 'e.scrollTope.scrollTope.scrollTop');
	})

	function gopage() {
		uni.switchTab({
			url: '/pages/index/index'
		})
	}
</script>

<style lang="scss">
	::v-deep .u-popup__content {
		background-color: rgba(255, 0, 0, 0) !important;
	}

	.popucenter {
		width: 592rpx;
		height: 720rpx;
		background: linear-gradient(180deg, #F4FFF5 0%, #FFFFFF 31%);
		border-radius: 28rpx 28rpx 28rpx 28rpx;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		align-items: center;

		.popucenterimg {
			width: 355.92rpx;
			height: 315.24rpx;
			margin-top: 22rpx;
		}

		.title {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #1D2129;
			margin-bottom: 24rpx;
		}

		.text {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #4E5969;
		}

		.btn {
			width: 422rpx;
			height: 64rpx;
			background: #7EBC79;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			text-align: center;
			line-height: 64rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #FFFFFF;
			margin-top: 22rpx;
		}

		.lookorder {
			width: 422rpx;
			height: 64rpx;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			border: 2rpx solid #7EBC79;
			text-align: center;
			line-height: 64rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			margin-top: 24rpx;
			color: #7EBC79;
		}
	}

	.fixedbox {
		width: 100%;
		overflow: hidden;
		height: 184rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -4rpx 4rpx 0rpx rgba(0, 0, 0, 0.08);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		position: fixed;
		bottom: 0%;
		z-index: 999;

		.btn {
			width: 686rpx;
			height: 88rpx;
			background: #7EBC79;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			text-align: center;
			line-height: 88rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 30rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 88rpx;
			margin: auto;
			margin-top: 22rpx;
		}
	}

	.yuyueinfo {
		width: 100%;
		overflow: hidden;
		position: relative;

		.topimage {
			width: 100%;
			height: 530rpx;
			background: linear-gradient(180deg, #84BD78 0%, rgba(132, 189, 120, 0) 100%);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			position: absolute;
			top: 0%;
			z-index: -1;
		}

		.yuyuebox {
			width: 686rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.15);
			border-radius: 24rpx 24rpx 24rpx 24rpx;
			margin: auto;
			margin-top: 32rpx;
			overflow: hidden;

			.center {
				width: 630rpx;
				margin: auto;

				.linebox1 {
					width: 100%;
					height: 84rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.xz {
						width: 32rpx;
						height: 32rpx;
					}

					.right {
						display: flex;
						font-size: 24rpx;
						color: #86909C;
						align-items: center;
					}

					.lefttext {
						display: flex;
						align-items: center;
						font-weight: 400;
						font-size: 28rpx;
						color: #1D2129;

						.iconsmall {
							width: 40rpx;
							height: 40rpx;
							margin-right: 12rpx;
						}
					}
				}

				.linebox {
					width: 100%;
					height: 84rpx;
					display: flex;
					align-items: center;
					border-bottom: 2rpx solid #F7F8FA;
					position: relative;

					.labletitle {
						width: 140rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #1D2129;
					}

					.righttitle {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #1D2129;
						position: relative;
						display: flex;
						align-items: center;
						margin-left: 20rpx;
						white-space: nowrap;
						width: 390rpx;
						/* 防止文本换行 */
						overflow: hidden;
						/* 隐藏超出容器的部分 */
						text-overflow: ellipsis;
						/* 超出部分显示为省略号 */
					}

					.bianji {
						right: 0%;
						width: 32rpx;
						height: 32rpx;
						position: absolute;
					}
				}

				.title {
					width: 100%;
					display: flex;
					align-items: center;
					margin-top: 34rpx;
					margin-bottom: 20rpx;

					.leftbox {
						width: 6rpx;
						height: 36rpx;
						background: #7EBC79;
						border-radius: 0rpx 24rpx 24rpx 0rpx;
					}

					.tt {
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 32rpx;
						color: #1D2129;
						margin-left: 12rpx;
					}
				}
			}
		}

		.infobox {
			width: 686rpx;
			height: 224rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 4rpx 4rpx 0rpx rgba(134, 202, 115, 0.43);
			border-radius: 24rpx 24rpx 24rpx 24rpx;
			margin: auto;
			display: flex;
			align-items: center;

			.leftimg {
				width: 198rpx;
				height: 176rpx;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				margin-left: 24rpx;
			}

			.texts {
				width: 412rpx;
				margin-left: 24rpx;
				overflow: hidden;
				white-space: nowrap;
				/* 防止文本换行 */
				overflow: hidden;
				/* 隐藏溢出的内容 */
				text-overflow: ellipsis;

				/* 显示省略号来代表被修剪的文本 */
				.t1 {
					width: 412rpx;
					display: flex;
					align-items: center;
					white-space: nowrap;
					/* 防止文本换行 */
					overflow: hidden;
					/* 隐藏溢出的内容 */
					text-overflow: ellipsis;

					/* 显示省略号来代表被修剪的文本 */
					.leftpobox {
						width: 56rpx;
						height: 40rpx;
						background: #7EBC79;
						border-radius: 4rpx 20rpx 4rpx 20rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 32rpx;
						color: #FFFFFF;
					}

					.title {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 32rpx;
						color: #1D2129;
						margin-left: 8rpx;
					}
				}

				.addressbox {
					display: flex;
					align-items: center;
					margin-top: 24rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #4E5969;

					image {
						width: 32rpx;
						height: 32rpx;
					}
				}

				.t2 {
					width: 412rpx;
					white-space: nowrap;
					overflow: hidden;
					font-weight: 400;
					font-size: 26rpx;
					color: #868686;
					margin-top: 26rpx;
					text-overflow: ellipsis;
				}
			}
		}
	}
</style>